home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
539
/
539.xpi
/
content
/
measureit
/
measureitOverlay.js
< prev
Wrap
Text File
|
2010-01-24
|
14KB
|
423 lines
if(!net) var net={};
if(!net.kevinfreitas) net.kevinfreitas={};
if(!net.kevinfreitas.measureit) net.kevinfreitas.measureit={};
( function() {
var pub = net.kevinfreitas.measureit;
pub.measureitStatus = 0;
pub.x1 = null;
pub.y1 = null;
// pub.x = window._content;
// var pageBody = pub.x.document.getElementsByTagName("html")[0];
pub.toggle = function() {
var measureitToolbar = document.getElementById("measureit_toolbar_button");
var measureitStatusBar = document.getElementById("measureit_statusbar");
if(pub.measureitStatus == 0) { // turn measureit on
pub.measureitOn();
if(measureitToolbar) {
measureitToolbar.setAttribute("checked", "true");
measureitToolbar.setAttribute("tooltiptext", "MeasureIt Off");
}
measureitStatusBar.style.backgroundColor = "#fff";
measureitStatusBar.style.backgroundPosition = "0 0";
measureitToggleTo = 1;
}
else if (pub.measureitStatus == 1) { // turn measureit off
pub.measureitOff();
if(measureitToolbar) {
measureitToolbar.setAttribute("checked", "false");
measureitToolbar.setAttribute("tooltiptext", "MeasureIt On");
}
measureitStatusBar.style.backgroundColor = "transparent";
measureitStatusBar.style.backgroundPosition = "-20px 0";
measureitToggleTo = 0;
}
pub.measureitStatus = measureitToggleTo;
}
pub.measureitOff = function() {
var x = window._content;
var pageBody = x.document.getElementsByTagName("html")[0];
x.document.removeEventListener("keypress", pub.keyboardControls, false);
var measureitVellum = x.document.getElementById("measureit");
var pageHead = x.document.getElementsByTagName("head")[0];
var measureitCSS = x.document.getElementById("measureit_css");
var measureitJS = x.document.getElementById("measureit_js");
if(measureitVellum != null) {
pageBody.removeChild(measureitVellum);
pageHead.removeChild(measureitCSS);
pageHead.removeChild(measureitJS);
}
}
pub.measureitOn = function() {
var x = window._content;
var pageBody = x.document.getElementsByTagName("html")[0];
x.document.addEventListener("keypress", pub.keyboardControls, false);
// insert header elements
pub.insertHeaderElements();
var measureItCheck = x.document.getElementById("measureit");
if(measureItCheck == null) {
var divMeasureit = x.document.createElement("div");
divMeasureit.setAttribute("id", "measureit");
var divMeasureitBack = x.document.createElement("div");
divMeasureitBack.setAttribute("id", "measureit_back");
divMeasureitBack.setAttribute("onmousedown", "net.kevinfreitas.measureit.drawStart(event)");
divMeasureit.appendChild(divMeasureitBack);
pageBody.appendChild(divMeasureit);
}
}
pub.insertHeaderElements = function() {
var x = window._content;
var pageBody = x.document.getElementsByTagName("html")[0];
var pageHead = x.document.getElementsByTagName("head")[0];
if(pageHead == null) { // if page head doesn't exist, create one
var pageHead = x.document.createElement("head");
pageBody.appendChild(pageHead);
var pageHead = x.document.getElementsByTagName("head")[0];
}
var cssCheck = x.document.getElementById("measureit_css");
var jsCheck = x.document.getElementById("measureit_js");
if(cssCheck == null) { // insert stylesheet reference
var linkMeasureitCSS = x.document.createElement("link");
linkMeasureitCSS.setAttribute("id", "measureit_css");
linkMeasureitCSS.setAttribute("rel", "stylesheet");
linkMeasureitCSS.setAttribute("type", "text/css");
linkMeasureitCSS.setAttribute("href", "chrome://measureit/skin/measureit.css");
pageHead.appendChild(linkMeasureitCSS);
}
if(jsCheck == null) { // insert javascript reference
var scriptMeasureitJS = x.document.createElement("script");
scriptMeasureitJS.setAttribute("id", "measureit_js");
scriptMeasureitJS.setAttribute("language", "JavaScript");
scriptMeasureitJS.setAttribute("type", "text/javascript");
scriptMeasureitJS.setAttribute("src", "chrome://measureit/content/measureitOverlay.js");
pageHead.appendChild(scriptMeasureitJS);
}
}
pub.drawStart = function(event) {
var x = window._content;
var measureItCheck = x.document.getElementById("measureit_proper");
if(measureItCheck == null) {
pub.rulerDOM();
}
// set initial point coordinates
tempX1 = event.clientX;
tempY1 = event.clientY;
event.preventDefault();
var toolbar = document.getElementById("measureit_toolbar");
toolbar.style.display = "none";
// set the position of the top left corner
var cropBox = document.getElementById("measureit_box");
cropBox.style.display = "none";
cropBox.style.left = tempX1 + "px";
cropBox.style.top = tempY1 + "px";
pub.x1 = tempX1;
pub.y1 = tempY1;
x.document.addEventListener("mousemove", pub.drawRuler, true);
x.document.addEventListener("mouseup", pub.drawRulerStop, true);
}
pub.drawRulerStop = function(event) {
var x = window._content;
x.document.removeEventListener("mousemove", pub.drawRuler, true);
x.document.removeEventListener("mouseup", pub.drawRulerStop, true);
}
pub.drawRuler = function(event) {
event.preventDefault();
var tempX, tempY, x2, y2;
var leftCorner = 0;
var tempX1 = pub.x1;
var tempY1 = pub.y1;
// alert("x1: " + tempX1 + " | y1: " + tempY1);
tempX = event.clientX;
tempY = event.clientY;
var cropBox = document.getElementById("measureit_box");
var leftCorner = 1;
var x2 = tempX;
var y2 = tempY;
// do the numbers
if(x2 > tempX1) {
var left = tempX1;
var width = (x2-tempX1)-1;
} else {
var left = x2;
var width = (tempX1-x2)-1;
}
var right = (left+width);
if(y2 > tempY1) {
var top = tempY1;
var height = (y2-tempY1)-1;
} else {
var top = y2;
var height = (tempY1-y2)-1;
}
// adjust cropBox with mouse movement
cropBox.style.display = "none";
cropBox.style.left = left + "px";
cropBox.style.top = top + "px";
if(width >= 0) {
cropBox.style.width = width + "px";
cropBox.style.borderRightWidth = "1px";
} else { // for 1px wide line
cropBox.style.width = "0px";
cropBox.style.borderRightWidth = "0";
}
if(height >= 0) {
cropBox.style.height = height + "px";
cropBox.style.borderBottomWidth = "1px";
} else {
cropBox.style.height = "0px";
cropBox.style.borderBottomWidth = "0";
}
cropBox.style.display = "block";
var toolbar = document.getElementById("measureit_toolbar");
toolbar.style.display = "block";
pub.placeToolbar(right, top);
pub.toolbarText(width, height);
}
pub.placeToolbar = function(posLeft, posTop) {
var x = window._content;
var rightBounds = window.innerWidth-21;
var toolbar = x.document.getElementById("measureit_toolbar");
toolbar.style.visibility = "hidden";
if(posLeft+124 < rightBounds) {
toolbar.style.left = posLeft + "px";
toolbar.style.top = posTop + "px";
} else { // ruler too close to right edge of browser
toolbar.style.left = rightBounds-124 + "px";
toolbar.style.top = posTop+14 + "px";
}
toolbar.style.visibility = "visible";
}
pub.toolbarText = function(width, height) {
var widthInfo = document.getElementById("measureit_width");
if(widthInfo.hasChildNodes() == 1) { widthInfo.removeChild(widthInfo.firstChild); }
var widthText = document.createTextNode("W: " + (width+2) + "px");
var heightInfo = document.getElementById("measureit_height");
if(heightInfo.hasChildNodes() == 1) { heightInfo.removeChild(heightInfo.firstChild); }
var heightText = document.createTextNode("H: " + (height+2) + "px");
widthInfo.appendChild(widthText);
heightInfo.appendChild(heightText);
}
pub.rulerDOM = function() {
var x = window._content;
var divMeasureit = x.document.getElementById("measureit");
var divMeasureitProper = x.document.createElement("div");
divMeasureitProper.setAttribute("id", "measureit_proper");
var divMeasureitBox = x.document.createElement("div");
divMeasureitBox.setAttribute("id", "measureit_box");
var divMeasureitBoxDiv = x.document.createElement("div");
divMeasureitBoxDiv.setAttribute("id", "measureit_ruler_top");
var divMeasureitBoxDivDiv = x.document.createElement("div");
divMeasureitBoxDivDiv.setAttribute("id", "measureit_ruler_left");
divMeasureitBoxDivDiv.addEventListener("mousedown", pub.dragStart, true);
var divMeasureitToolbar = x.document.createElement("div");
divMeasureitToolbar.setAttribute("id", "measureit_toolbar");
var aMeasureitClear = x.document.createElement("a");
aMeasureitClear.setAttribute("id", "measureit_clear");
aMeasureitClear.setAttribute("onclick", "net.kevinfreitas.measureit.clearCrop()");
aMeasureitClear.setAttribute("title", "Click to remove this ruler");
var spanWidth = x.document.createElement("span");
spanWidth.setAttribute("id", "measureit_width");
var spanHeight = x.document.createElement("span");
spanHeight.setAttribute("id", "measureit_height");
divMeasureitToolbar.appendChild(aMeasureitClear);
divMeasureitToolbar.appendChild(spanWidth);
divMeasureitToolbar.appendChild(spanHeight);
divMeasureitBoxDiv.appendChild(divMeasureitBoxDivDiv);
divMeasureitBox.appendChild(divMeasureitBoxDiv);
divMeasureitProper.appendChild(divMeasureitBox);
divMeasureitProper.appendChild(divMeasureitToolbar);
divMeasureit.appendChild(divMeasureitProper);
}
pub.clearCrop = function() {
var x = window._content;
// hide everything
cropBox = x.document.getElementById("measureit_box");
cropBox.style.display = "none";
toolbar = x.document.getElementById("measureit_toolbar");
toolbar.style.display = "none";
}
// inspired by "Generic Drag" by Mike Hall at http://www.brainjar.com/dhtml/drag/
pub.dragBox = new Object();
pub.dragStart = function(event) {
var x = window._content;
pub.dragBox.clickedBox = event.target; // get object that was clicked on
pub.dragBox.measureitBox = x.document.getElementById("measureit_box");
pub.dragBox.measureitToolbar = x.document.getElementById("measureit_toolbar");
pub.dragBox.rightBounds = window.innerWidth-21;
pub.dragBox.cursorX1 = event.clientX;
pub.dragBox.cursorY1 = event.clientY;
pub.dragBox.boxStartLeft = parseInt(document.defaultView.getComputedStyle(pub.dragBox.measureitBox, "").getPropertyValue("left"));
pub.dragBox.boxStartTop = parseInt(document.defaultView.getComputedStyle(pub.dragBox.measureitBox, "").getPropertyValue("top"));
pub.dragBox.measureitBox.setAttribute("class", "dragging");
document.addEventListener("mousemove", pub.dragGo, true);
document.addEventListener("mouseup", pub.dragStop, true);
event.preventDefault();
}
pub.dragGo = function(event) {
var x2, y2;
x2 = event.clientX;
y2 = event.clientY;
var newLeft = pub.dragBox.boxStartLeft + x2 - pub.dragBox.cursorX1;
var newTop = pub.dragBox.boxStartTop + y2 - pub.dragBox.cursorY1;
if(newLeft < 0) newLeft = 0;
if(newTop < 0) newTop = 0;
var newRight = parseInt(document.defaultView.getComputedStyle(pub.dragBox.measureitBox, "").getPropertyValue("width")) + newLeft + 2;
pub.dragBox.measureitBox.style.visibility = "hidden";
pub.dragBox.measureitBox.style.left = newLeft + "px";
pub.dragBox.measureitBox.style.top = newTop + "px";
pub.dragBox.measureitBox.style.visibility = "visible";
// placeToolbar(newRight, newTop);
// var rightBounds = window.innerWidth-21;
pub.dragBox.measureitToolbar.style.visibility = "hidden";
if(newRight+124 < pub.dragBox.rightBounds) {
pub.dragBox.measureitToolbar.style.left = newRight + "px";
pub.dragBox.measureitToolbar.style.top = newTop + "px";
} else { // ruler too close to right edge of browser
pub.dragBox.measureitToolbar.style.left = pub.dragBox.rightBounds-124 + "px";
pub.dragBox.measureitToolbar.style.top = newTop+14 + "px";
}
pub.dragBox.measureitToolbar.style.visibility = "visible";
}
pub.dragStop = function(event) {
pub.dragBox.measureitBox.setAttribute("class", null);
document.removeEventListener("mousemove", pub.dragGo, true);
document.removeEventListener("mouseup", pub.dragStop, true);
}
pub.moveRuler = function(xDist, yDist) {
var x = window._content;
var measureitBox = x.document.getElementById("measureit_box");
var boxStartLeft = parseInt(x.document.defaultView.getComputedStyle(measureitBox, "").getPropertyValue("left"));
var boxStartTop = parseInt(x.document.defaultView.getComputedStyle(measureitBox, "").getPropertyValue("top"));
var newLeft = boxStartLeft + xDist;
var newTop = boxStartTop + yDist;
if(newLeft < 0) newLeft = 0;
if(newTop < 0) newTop = 0;
var newRight = parseInt(x.document.defaultView.getComputedStyle(measureitBox, "").getPropertyValue("width")) + newLeft + 2;
var rightBounds = window.innerWidth-21;
measureitBox.style.visibility = "hidden";
measureitBox.style.left = newLeft + "px";
measureitBox.style.top = newTop + "px";
measureitBox.style.visibility = "visible";
var measureitToolbar = x.document.getElementById("measureit_toolbar");
measureitToolbar.style.visibility = "hidden";
if(newRight+124 < rightBounds) {
measureitToolbar.style.left = newRight + "px";
measureitToolbar.style.top = newTop + "px";
} else { // ruler too close to right edge of browser
measureitToolbar.style.left = rightBounds-124 + "px";
measureitToolbar.style.top = newTop+14 + "px";
}
measureitToolbar.style.visibility = "visible";
}
pub.keyboardControls = function(event) {
var pressedKey = event.keyCode;
var pressedCtrl = event.ctrlKey;
var pressedShft = event.shiftKey;
event.preventDefault();
if(pressedCtrl) var offsetValue = 1;
if(pressedShft) var offsetValue = 5;
if(pressedKey == 27) pub.toggle(); // ESC key
if(pressedCtrl) {
switch(pressedKey) {
case 37: pub.moveRuler(-offsetValue, 0); break; // left arrow
case 39: pub.moveRuler(offsetValue, 0); break; // right arrow
case 38: pub.moveRuler(0, -offsetValue); break; // up arrow
case 40: pub.moveRuler(0, offsetValue); break; // down arrow
}
}
}
})();
var mi = net.kevinfreitas.measureit;